<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-dropdown v-if="selectedRowKeys.length > 0 && hasPermission(['admin', 'wf:processInstance:withdraw'])">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="handleBatchWithdraw">
                <Icon icon="ant-design:rollback-outlined" />
                撤回
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="ant-design:down-outlined" />
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <ProcessInstanceDetailDrawer @register="registerDetailDrawer" />
  </div>
</template>

<script lang="ts" name="system-position" setup>
  import { ref, unref } from 'vue';
  import { BasicTable, useTable, TableAction, ActionItem } from '/@/components/Table';
  import { list, withdrawProcessInstance } from './processinstance.api';
  import { columns, searchFormSchema } from './processinstance.data';
  import { useDrawer } from '/@/components/Drawer';
  import ProcessInstanceDetailDrawer from './ProcessInstanceDetailDrawer.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { usePermission } from '/@/hooks/web/usePermission';

  const { createMessage } = useMessage();
  const { hasPermission } = usePermission();
  const [registerDetailDrawer, { openDrawer: openDetailDrawer }] = useDrawer();

  // 列表页面公共参数、方法
  const { tableContext } = useListPage({
    designScope: 'process-instance-template',
    tableProps: {
      title: '我的发起列表',
      api: list,
      columns: columns,
      formConfig: {
        schemas: searchFormSchema,
      },
      actionColumn: {
        width: 200,
      },
      showIndexColumn: true,
      beforeFetch: handleBeforeFetch,
    },
  });

  const [registerTable, { reload, clearSelectedRowKeys }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 操作列定义
   * @param record
   */
  function getActions(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
        auth: ['admin', 'wf:processInstance:detail'], // 根据权限控制是否显示: 无权限，不显示
      },
    ];
  }

  /**
   * 下拉操作栏
   */
  function getDropDownAction(record): ActionItem[] {
    return [
      {
        label: '撤回',
        popConfirm: {
          title: '确定撤回流程吗？',
          confirm: handleWithdraw.bind(null, record),
        },
        disabled: record.state !== 10,
        auth: ['admin', 'wf:processInstance:withdraw'], // 根据权限控制是否显示: 无权限，不显示
      },
    ];
  }

  // 撤回
  function handleWithdraw(record) {
    const ids: Array<any> = [];
    if (record && record.id) {
      ids.push(record.id);
    }
    withdrawProcessInstance({
      ids: ids,
    }).then(() => {
      createMessage.success('撤回成功');
      clearSelectedRowKeys();
      reload();
    });
  }

  // 多选撤回
  function handleBatchWithdraw() {
    const ids: Array<any> = [];
    ids.push(...unref(selectedRowKeys));
    withdrawProcessInstance({
      ids: ids,
    }).then(() => {
      createMessage.success('撤回成功');
      clearSelectedRowKeys();
      reload();
    });
  }

  // 处理查询请求参数
  function handleBeforeFetch(params) {
    if (params) {
      Object.keys(params).forEach((key) => {
        if (params[key] == 9999999999) {
          // 删除全部自定义的值-不提交到后端
          delete params[key];
        }
      });
    }
    return params;
  }

  /**
   * 打开流程详情
   * @param {} record
   */
  function handleDetail(record) {
    openDetailDrawer(true, {
      record,
    });
  }
</script>

<style lang="less" scoped></style>
